using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

public static class PathExtension
{
    public static string GetAbsPath(this string relaPath)
    {
        return Regex.Replace(relaPath, @"^Assets", Application.dataPath);
    }

    public static string GetParentPath(this string path)
    {
        var fileName = Path.GetFileName(path);
        return Regex.Replace(path.Format(), @"/" + fileName + "$", "");
    }

    public static string GetRelaPath(this string absPath)
    {
        return Regex.Replace(absPath, @"^" + Application.dataPath, "Assets");
    }

    public static bool CheckExtension(this string path, string ext)
    {
        if (!Path.GetExtension(path).Equals(ext))
        {
            return false;
        }

        return true;
    }

    public static bool CheckExists<T>(this List<T> objs) where T : Object
    {
        for (int i = 0; i < objs.Count; i++)
        {
            var path = AssetDatabase.GetAssetPath(objs[i]);
            if (!File.Exists(path.GetAbsPath()))
                return false;
        }

        return true;
    }

    /// <summary>
    /// \\ 2 /
    /// </summary>
    /// <returns></returns>
    public static string Format(this string path)
    {
        return path.Replace("\\", "/");
    }

    public static List<T> GetAllFiles<T>(string path, string type, bool isDeep = false) where T : UnityEngine.Object
    {
        var result = new List<T>();

        var files = Directory.GetFiles(path, type, isDeep ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
        for (var i = 0; i < files.Length; i++)
        {
            var file = files[i].GetRelaPath();
            result.Add(AssetDatabase.LoadAssetAtPath<T>(file));
        }

        return result;
    }
    
    public static List<string> GetAllFiles(string path, string type)
    {
        var result = new List<string>();

        var files = Directory.GetFiles(path, type, SearchOption.TopDirectoryOnly);
        for (var i = 0; i < files.Length; i++)
        {
            result.Add(files[i]);
        }

        return result;
    }

    public static List<string> GetAllFolder(string path, string type)
    {
        var result = new List<string>();

        var files = Directory.GetDirectories(path, type, SearchOption.TopDirectoryOnly);
        for (var i = 0; i < files.Length; i++)
        {
            result.Add(files[i]);
        }

        return result;
    }
}